package com.ptbuy.db.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ptbuy.db.pojo.ClientPojo;
import com.ptbuy.utils.Utils;
import com.ptbuy.base.BaseApplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ClientDao {
    public SQLiteDatabase sqLiteOpenHelper;

    public ClientDao() {
        sqLiteOpenHelper = BaseApplication.getDb();
    }


    /**
     * 新增客户信息
     *
     * @return
     */
    public synchronized int addInfo(String uuid, String name, String agentName, String totalProduct, String totalPrice, String phone, String singaturePath) {
        sqLiteOpenHelper.beginTransaction();
        try {
            Date date = new Date();
            String currentTime = Utils.getCurrentTime(date);
            ContentValues values = new ContentValues();
//            String uuid = UUID.randomUUID().toString().replace("-", "");
            values.put("id", uuid);
            values.put("name", name);
            values.put("agent_name", agentName);
            values.put("total_product", totalProduct);
            values.put("total_price", totalPrice);
            values.put("time", currentTime);
            values.put("phone", phone);
            values.put("signatureFilePath", singaturePath);
            long insertCount = sqLiteOpenHelper.insert("client", null, values);
            sqLiteOpenHelper.setTransactionSuccessful();
        } finally {
            sqLiteOpenHelper.endTransaction();
        }
        return 1;
    }

    //删除客户信息
    public synchronized int delInfo(String id) {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("client", "id=?", new String[]{id});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }

    public synchronized List<ClientPojo> queryRecodInfo(int offset, int pageSize, String sqlParam) {
        List<ClientPojo> list = new ArrayList<>();
        try {
            String sql = "select id,name,agent_name,total_product,total_price,time,phone,signatureFilePath from client order by myorder desc";
            Cursor cursor = null;
            if (sqlParam != null && !"".equalsIgnoreCase(sqlParam)) {
                sql = "select id,name,agent_name,total_product,total_price,time,phone,signatureFilePath  from client where " + sqlParam + " order by myorder desc";
            }
            if (pageSize > 0) {
                sql += "  LIMIT " + pageSize + " OFFSET " + offset;
            }
            cursor = sqLiteOpenHelper.rawQuery(sql, null);
            ClientPojo recodInfo = null;
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ClientPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
                    recodInfo.setAgentName(cursor.getString(cursor.getColumnIndex("agent_name")));
                    recodInfo.setTotalProduct(cursor.getInt(cursor.getColumnIndex("total_product")));
                    recodInfo.setTotalPrice(cursor.getString(cursor.getColumnIndex("total_price")));
                    String time = cursor.getString(cursor.getColumnIndex("time"));
                    recodInfo.setTime(time);
                    recodInfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                    recodInfo.setSignatureFilePath(cursor.getString(cursor.getColumnIndex("signatureFilePath")));
                    list.add(recodInfo);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }

    //通过id查询
    public synchronized ClientPojo queryClientInfo(String id) {
        ClientPojo recodInfo = null;
        try {
            String sql = "select id,name,agent_name,total_product,total_price,time,phone,signatureFilePath from client where id=?";
            Cursor cursor = null;
            cursor = sqLiteOpenHelper.rawQuery(sql, new String[]{id});
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ClientPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
                    recodInfo.setAgentName(cursor.getString(cursor.getColumnIndex("agent_name")));
                    recodInfo.setTotalProduct(cursor.getInt(cursor.getColumnIndex("total_product")));
                    recodInfo.setTotalPrice(cursor.getString(cursor.getColumnIndex("total_price")));
                    recodInfo.setTime(cursor.getString(cursor.getColumnIndex("time")));
                    recodInfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                    recodInfo.setSignatureFilePath(cursor.getString(cursor.getColumnIndex("signatureFilePath")));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return recodInfo;
    }

    /**
     * 查询总价多少
     *
     * @return
     */
    public synchronized List<String> queryTotalPriceInfo() {
        List<String> list = new ArrayList<>();
        try {
            String sql = "select total_price from client ";
            Cursor cursor = null;
            cursor = sqLiteOpenHelper.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                do {
                    String total_price = cursor.getString(cursor.getColumnIndex("total_price"));
                    String replace = total_price.replace(",", "");
                    list.add(replace);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }

    public synchronized Integer queryCount() {
        Integer total = 0;
        try {
            String sql = "select count(id) id  from client ";
            Cursor cursor = null;
            cursor = sqLiteOpenHelper.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                do {
                    total = cursor.getInt(cursor.getColumnIndex("id"));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return total;
    }

    /**
     * 查询总件数
     *
     * @return
     */
    public synchronized Integer queryTotalProductInfo() {
        Integer product = 0;
        try {
            String sql = "select sum(total_product) totalProduct from client ";
            Cursor cursor = null;
            cursor = sqLiteOpenHelper.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                do {
                    product = cursor.getInt(cursor.getColumnIndex("totalProduct"));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return product;
    }

    /**
     * 修改
     */
    public synchronized Integer updateSumInfo(String pid, String total_product, String total_price) {
        try {
            ContentValues values = new ContentValues();
            values.put("total_product", total_product);
            values.put("total_price", total_price);
            String[] args = {pid};
            int update = sqLiteOpenHelper.update("client", values, "id=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 修改签字确认图片
     *
     * @param pid
     * @param signatureFilePath
     * @return
     */
    public synchronized Integer updateSignatureFilePath(String pid, String signatureFilePath) {
        try {
            ContentValues values = new ContentValues();
            values.put("signatureFilePath", signatureFilePath);
            String[] args = {pid};
            int update = sqLiteOpenHelper.update("client", values, "id=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 信息修改
     *
     * @param id
     * @param name
     * @param agent_name
     * @param phone
     * @return
     */
    public synchronized Integer updateClientInfo(String id, String name, String agent_name, String phone) {
        try {
            ContentValues values = new ContentValues();
            values.put("name", name);
            values.put("agent_name", agent_name);
            values.put("phone", phone);
            String[] args = {id};
            int update = sqLiteOpenHelper.update("client", values, "id=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

}
